package br.com.portalecbahia;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import br.com.portalecbahia.connectionservices.SynchronizePlayer;
import br.com.portalecbahia.customize.HorizontalPager;
import br.com.portalecbahia.dao.PlayerDAO;
import br.com.portalecbahia.listner.PlayersListeners;
import br.com.portalecbahia.model.Player;

import com.google.analytics.tracking.android.EasyTracker;
import com.raptureinvenice.webimageview.image.WebImageView;

public class PlayerActivity extends Activity implements PlayersListeners {
	

	private List<Player> listPlayer;
	private PlayerDAO dao;
	private HorizontalPager realViewSwitcher;
	private SynchronizePlayer taskPlayer;
	private TextView titleTopTX;
	private TextView pageTX;
	private TextView maxTX;
	private ImageButton expandBTN;
	public int first = 0;
	private ScrollView mainScrollView;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.player_list);

		EasyTracker.getInstance().setContext(this);
	    realViewSwitcher  = (HorizontalPager) findViewById(R.id.listviewplayers);
	   
		dao = new PlayerDAO(this);
		


		
		titleTopTX = (TextView)findViewById(R.id.titleTopTX);		
		pageTX= 	 (TextView)findViewById(R.id.pageTX);
		maxTX = 	 (TextView)findViewById(R.id.maxTX);


		taskPlayer = new SynchronizePlayer(this);
		taskPlayer.execute();
		
		buildScreen();

		 

    }
    
    @Override
    public void onStart() {
      super.onStart();
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
      super.onStop();
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
	
	public void buildScreen(){
		
		listPlayer =  dao.findAll();
		maxTX.setText(listPlayer.size()+"");
		int page=0;
		for (int i = 0; i < listPlayer.size(); i++) {
			LayoutInflater inflater = (LayoutInflater)
			this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(R.layout.item_list_horizontal_player, null);
			Player player  = listPlayer.get(i);
			if(player.getUrlImage()!=null && !"".equals(player.getUrlImage())){
				WebImageView myImage = (WebImageView)view.findViewById(R.id.playerIMG);
				WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
				myImage.setImageWithURL(this, player.getUrlImage());
			
			}
	            
			
	            
			WebView descriptionTX = (WebView)view.findViewById(R.id.descriptionWV);
			descriptionTX.loadDataWithBaseURL(null, player.getDescription(), "text/html", "utf-8", null);
			WebSettings webSettings = descriptionTX.getSettings();
			webSettings.setDefaultFontSize(13);
			page = 0;
			if(i==0){
			titleTopTX.setText(player.getName()+" - "+player.getPosition());
			}
	        
	        
	        realViewSwitcher.addView(view);
		}

		if(listPlayer!=null && listPlayer.size()>0){
		pageTX.setText(page+1+"");

		realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
		
	    realViewSwitcher.setCurrentScreen(page, false);

		}else{
			realViewSwitcher.setEnabled(false);
		}
	}
	
	public static Drawable LoadImageFromWebOperations(String url) {
	    try {
	        InputStream is = (InputStream) new URL(url).getContent();
	        Drawable d = Drawable.createFromStream(is, "src name");
	        return d;
	    } catch (Exception e) {
	        return null;
	    }
	}
	
	private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =  new HorizontalPager.OnScreenSwitchListener() {
           @Override
           public void onScreenSwitched(final int screen) {
               // Check the appropriate button when the user swipes screens.
        		
   			int indice = screen;
   	        if(screen == listPlayer.size()){
   	        	buildScreen();
   	        	return;
   	        }
   			Player player  = listPlayer.get(indice);
   	                	
   			
   			
			titleTopTX.setText(player.getName()+" - "+player.getPosition());
   	                    
   			
   			pageTX.setText(indice+1+"");
   			
           }

		
       };

	@Override
	public void onSynchronizePlayerComplete(Boolean response) {
		
			
		buildScreen();
		
	}
	  
	
	            
	public void openImage(View view){
		
		//set up dialog
        Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.expand_image);
        dialog.setCancelable(true);

        //set up image view
        WebImageView       webView= (WebImageView) dialog.findViewById(R.id.expandIMG);


        webView.setImageWithURL(this, view.getContentDescription()+"");

 
        dialog.show();
	}
	           
}
